package com.luoyi.util.mpaas.common;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class EasyExcelUtil {

    /**
     * 自动行合并 附属字段
     */
    public static final String AUTO_ROW_MERGE_OTHER_FIELD = "{&autoRowMergeOtherField}";
    /**
     * 自动行合并
     */
    public static final String AUTO_ROW_MERGE = "{&autoRowMerge}";
    /**
     * 自动行合并主字段
     */
    public static final String AUTO_ROW_MERGE_MAIN_FIELD = "{&autoRowMergeMainField}";
    /**
     * 电子印章图片占位符号
     */
    public static final String ELECTRONIC_SEAL_PICTURE = "{&electronicSealPicture}";
    /**
     * 动态行高
     */
    public static final String ADAPTIVE_ROW_HEIGHT = "{&adaptiveRowHeight}";


    /**
     * 忽略的字段
     */
    private List<String> IGORE_PARAM_LIST = ListUtil.of(AUTO_ROW_MERGE_OTHER_FIELD, AUTO_ROW_MERGE, AUTO_ROW_MERGE_MAIN_FIELD, ELECTRONIC_SEAL_PICTURE, ADAPTIVE_ROW_HEIGHT);


    /**
     * excel模板生成mock数据
     *
     * @return 解析后的mock数据
     */
    public JSONArray parseJsonMock(FileInputStream fileInputStream) {
        ExcelReader reader = ExcelUtil.getReader(fileInputStream);
        Map<String, List<List<Object>>> map = new LinkedHashMap<>();
        for (Sheet sheet : reader.getSheets()) {
            String sheetName = sheet.getSheetName();
            List<List<Object>> list = reader.read();
            map.put(sheetName, list);
        }
        reader.close();
        JSONArray result = new JSONArray();
        map.forEach((sheetName, list) -> {
            JSONObject jsonItem = new JSONObject();
            jsonItem.put("sheetName", sheetName);
            JSONObject param = new JSONObject();
            jsonItem.put("data", param);
            for (int i = 0; i < list.size(); i++) {
                List<Object> objects = list.get(i);
                for (int j = 0; j < objects.size(); j++) {
                    Object itemVal = objects.get(j);
                    if (!(itemVal instanceof String)) {
                        log.warn("sheetName={}, 非字符串类型，无法解析：{}", itemVal);
                        continue;
                    }
                    String stringVal = itemVal.toString();
                    if (stringVal.contains(ELECTRONIC_SEAL_PICTURE)) {
                        // 图片展位符号
                        // todo

                    }

                    if (containsDotBetweenBraces(stringVal)) {
                        // 数组参数

                    }


                    boolean singleParam = containsSingleParam(stringVal);
//                    param.put(string, null);
                }
            }
        });
        return result;
    }



    private static String getCellParam(String stringCellValue) {
        String regex = "\\{(.*?)\\}";
        List<String> all = ReUtil.findAll(regex, stringCellValue.replace(AUTO_ROW_MERGE_OTHER_FIELD, ""), 0, new ArrayList<>());
        return all.get(0).replace("{", "").replace("}", "");
    }



    /**
     * 判断{}中带有.
     * @param string
     * @return
     */
    private static boolean containsDotBetweenBraces(String string) {
        String regex = "\\{.*\\..*\\}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);
        return matcher.find();
    }

    /**
     * 判断字符包含{}格式
     * @param string 字符
     * @return 是否包含
     */
    private static boolean containsSingleParam(String string) {
        String regex = "\\{.*?\\}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);
        return matcher.find();
    }

    /**
     * 获取{}中带有.的值
     * @param string
     * @return
     */
    private static String containsDotBetweenBracesString(String string) {
        String regex = "\\{(.*?)\\}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);
        while (matcher.find()) {
            String match = matcher.group(0);
            return match;
        }
        return null;
    }


}
